package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.eascs.admin.common.utils.AccountConstants;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.loan.service.O2oChargeSituationService;
import com.eascs.admin.o2o.loanAccount.entity.O2oCollectFee;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.afterloan.entity.O2oBankLoanDataTimer;
import com.eascs.afterloan.service.DeclareSignService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 个人打款流程
 */
@Service
public class CollectFeesMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(CollectFeesMsgNotifyServiceImpl.class);

	@Autowired
	IO2oCollectFeesService o2oCollectFeesService;
	@Autowired
	DeclareSignService declareSignService;
	@Autowired
	O2oChargeSituationService o2oChargeSituationService;

	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply) {
		ModelResult<O2oLoanapplyEntity> modelResult = new ModelResult<>();
		log.info("个人打款消息通知:[{}]", notifyParam.getProcessType());
		loanapply = txFlow(notifyParam, loanapply);
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("ACCOUNT_REMITTANCE");
		return list;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}

	/**
	 * 个人打款流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	private O2oLoanapplyEntity txFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply) {
		O2oCollectFee entity = o2oCollectFeesService.queryById(notifyParam.getReferId());
		if (entity == null) {
			log.debug("个人打款对象为空，传递id：ReferId:[{}]", notifyParam.getReferId());
			new RuntimeException("个人打款对象为空");
		}
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("个人打款流程通过：ReferId:[{}]", notifyParam.getReferId());
			entity.setStatus(StatusContexts.WFTYPE_STATUS_CLO);
			int i = o2oCollectFeesService.update(entity);
			if (i > 0) {
				log.info("ReferId:[{}],更新个人打款表状态成功", notifyParam.getReferId());

				String reulst = o2oCollectFeesService.personal_collectFees(entity, AccountConstants.ACCOUNT_SF);
				if (StringUtil.isNotBlank(reulst) && reulst.equals("SUCCESS")) {
					log.info("个人打款流程通过，调用数据账户支付成功");
//					DeclareSignEntity declareSignEntity =declareSignService.queryByCollectId(notifyParam.getReferId());
					O2oBankLoanDataTimer bankLoanDataTimer = declareSignService.queryByCollectId(notifyParam.getReferId());
					if (null != bankLoanDataTimer) {
						bankLoanDataTimer.setFlowStatus(StatusContexts.O2O_YES);
						log.info("更新导入确认放款表状态为流程已通过");
						int j = declareSignService.updateById(bankLoanDataTimer);
						log.info("更新导入确认放款表状态为流程已通过是否更新成功：【{}】", j);
						// o2oChargeSituationService
						log.info("调用service封装扣费接口");
						LoanapplyExpand expand = o2oChargeSituationService.queryExpandByLoanapplyNo(bankLoanDataTimer.getLoanapplyNo());
						log.info("查询收费情况：【{}】", JSONObject.toJSONString(expand));
						log.info("调用service封装扣费接口：扣费，转移到公司账户");
						String result = o2oChargeSituationService.Charging(expand, "KF");
						log.info("调用service封装扣费接口，扣费，转移到公司账户结果：【{}】", result);
					}
				} else {
					log.info("ReferId:[{}],个人打款流程通过，调用数据账户支付失败", notifyParam.getReferId());
					new RuntimeException("个人打款流程通过，调用数据账户支付失败");
				}
			} else {
				log.info("ReferId:[{}],更新个人打款表状态失败", notifyParam.getReferId());
			}

		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("个人打款流程驳回：ReferId:[{}]", notifyParam.getReferId());
			entity.setStatus(StatusContexts.BZJ_STATUS_NEW);
			int i = o2oCollectFeesService.update(entity);
			if (i > 0) {
				log.info("ReferId:[{}],驳回个人打款成功", notifyParam.getReferId());
			} else {
				log.info("ReferId:[{}],驳回个人打款失败", notifyParam.getReferId());
			}
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("个人打款流程作废：ReferId:[{}]", notifyParam.getReferId());
		}
		return loanapply;
	}

}
